﻿<html>

	<head>
		<title>优惠券详情</title>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">

		<link href="../../css/skin/default/style.css" rel="stylesheet" type="text/css" />
		<link href="../../css/dialog.css" rel="stylesheet">
		<link href="../../js/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />

		<script src="../../js/lib/jquery/jquery-1.10.2.min.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>

		<script src="../../js/lib/ligerUI/js/plugins/ligerPopupEdit.js"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerForm.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerCheckBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerComboBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDateEditor.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerRadio.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerSpinner.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTree.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerResizable.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTip.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerMenu.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script>
		<script src="../../js/lib/jquery.form.js" type="text/javascript"></script>
		<script src="../../js/lib/json2.js" type="text/javascript"></script>
		<script src="../../js/dialog.js"></script>
		<script src="../../js/datetool.js"></script>

		<script src="../../js/constants.js" type="text/javascript"></script>
		<script src="../../js/api.js" type="text/javascript"></script>
	</head>
	<script type="text/javascript">
		var manager;
		var data;
		var token = $api.getStorage("token");
		var couponUrl = BaseUrl + "Coupon?token=" + token;

		$(function() {
			$("form").ligerForm();
			data = JSON.parse(unescape($api.getparastr("data")));

			initView();
		});

		function showGird(url) {
			manager = $("#maingrid4").ligerGrid({
				columns: [{
						display: '标题',
						name: 'title',
						align: 'left',
						width: 280,
						render: function(rowdata, rowindex, value) {
							var ret = "<img src=" + rowdata.Fhimg + " class='listimgs' style='float:left;top:5px' />";
							ret += "<div style='float:left; margin-left:5px'><span  >" + rowdata.Fsummary + "</span><br/>";

							ret += "<div>"
							return ret;
						}
					},
					{
						display: '货号',
						name: 'Fpcode',
						align: 'left',
						width: 120,
						isSort: true
					},
					{
						display: '条码',
						name: 'Fbarcode',
						align: 'left',
						width: 120
					},
					{
						display: '售价',
						name: 'Fsaleprice',
						isSort: true,
						minWidth: 140,
						align: 'left'
					},
					{
						display: '操作',
						name: '',
						align: 'left',
						width: 120,
						render: function(rowdata, rowindex, value) {
							return "<a style='cursor:pointer;'   onclick='deleteRow(" + rowindex + ");'>删除</a>";
						}
					}
				],
				dataAction: 'local',
				url: url,
				method: 'get',
				width: '99.5%',
				pageSize: 50,
				rowHeight: 100,
				isScroll: true,
				usePager: true //是否分页   
			});
		}

		function deleteRow(i) {
			manager.deleteRow(i);
		}

		var sdtManager, edtManager, FctypeManager, FsdtypeManager;

		function initView() {
			sdtManager = $("#Fsdate").ligerGetDateEditorManager({
				absolute: false,
				format: "yyyy-MM-dd"
			});
			edtManager = $("#Fedate").ligerGetDateEditorManager({
				absolute: false,
				format: "yyyy-MM-dd"
			});

			FctypeManager = $("#Fctype").ligerComboBox({
				width: 180,
				selectBoxWidth: 200,
				selectBoxHeight: 200,
				onSelected: function(newvalue) {
					//优惠券
					if(newvalue == 1) {
						$("#yh").css("display", "block");
						$("#dy").css("display", "none");
						$("#timeType").css("display", "block");

						$("#product").css("display", "block");
					} else {
						//抵用券
						$("#timeType").css("display", "none");
						$("#dy").css("display", "block");
						$("#yh").css("display", "none");
						$("#product").css("display", "none");

					}

				}

			});
			FsdtypeManager = $("#Fsdtype").ligerComboBox({
				width: 180,
				selectBoxWidth: 200,
				selectBoxHeight: 200,
				onSelected: function(newvalue) {
					if(newvalue == 1) {
						$("#day").css("display", "none");
						$("#muitFday").css("display", "block");
					} else {
						$("#muitFday").css("display", "none");
						$("#day").css("display", "block");
					}
				}

			});
			$("#Fsummary").bind("input propertychange change", function(event) {
				if(event.type != "propertychange" || event.originalEvent.propertyName == "value") {
					lastword();
				}
			});
			if(!$api.isEmpty(data)) {

				$api.loadJsonToForm(data);
				if(data.Fcurrnum > 0) {
					$api.showtoast("该优惠劵以被领取，不能修改！");
					$("#btnsave").css("display", "none");
				}
				FctypeManager.setValue(data.Fctype);
				
				if(data.Fctype == 1) {
					FsdtypeManager.setValue(data.Fsdtype);
					$("#yh").css("display", "block");
					$("#dy").css("display", "none");
					$("#timeType").css("display", "block");
					if(data.Fsdtype == 1) {
						$("#day").css("display", "none");
						$("#muitFday").css("display", "block");
						var index = data.Fsdate.indexOf("T");
						$("#Fsdate").val(data.Fsdate.substr(0, index));
						index = data.Fedate.indexOf("T");
						$("#Fedate").val(data.Fedate.substr(0, index));
					} else {
						$("#muitFday").css("display", "none");
						$("#day").css("display", "block");
					}
				} else {
					//抵用券
					$("#timeType").css("display", "none");
					$("#dy").css("display", "block");
					$("#yh").css("display", "none");
					$("#product").css("display", "none");

					$("#Ffacevalue2").val(data.Ffacevalue);
					$("#Ffullvalue2").val(data.Ffullvalue);
				}

				var url = "";
				if(!$api.isEmpty(data.Fpids)) {
					url = BaseUrl + "Spu?token=" + $api.getStorage("token") + "&pids=" + data.Fpids;

				}
				showGird(url);
			} else {
				var dataUtil = new datetool();
				var date = dataUtil.getCurrentMonth();

				edt = date[1].format("yyyy-MM-dd");
				$("#Fsdate").val(dataUtil.getCurrentDate().format("yyyy-MM-dd"));
				$("#Fedate").val(edt);
				showGird("");
			}

		}
		var dialog;

		function add() {
			dialog = parent.f_ProductWin('isMuit=true');

		}

		function putProducts(rows) {
			for(var i = 0; i < rows.length; i++) {
				var row = rows[i];
				if(!isExists(row))
					manager.addRow(row);
			}
		}

		function saveInfo(row, cmd) {
			if(!isExists(row))
				manager.addRow(row);
		}

		function isExists(addRow) {
			for(var i in manager.rows) {
				var row = manager.getRow(i);
				if(row.Fid == addRow.Fid)
					return true;
			}
			return false;
		}

		function lastword() {
			var len = $("#Fsummary").val().length;
			if(len >= 30)
				$("#lnum").text("还可以输入0个字符");
			else
				$("#lnum").text("还可以输入" + (30 - len) + "个字符");

		}

		function doSaveInfo() {
			var Fsummary = $('#Fsummary').val();
			if($api.isEmpty(Fsummary)) {
				top.$.ligerDialog.error('优惠券摘要不能为空！');
				return;
			}
			var ctype = FctypeManager.getValue();
			//优惠券时判断
			if(ctype == 1) {
				var sdtype = FsdtypeManager.getValue();
				if(sdtype == 1) {
					if($api.DateDiff($('#Fsdate').val()) < 0) {
						top.$.ligerDialog.error('开始日期必须大于当前日期！');
						return;
					}

					if($api.DateCompare($('#Fsdate').val(), $('#Fedate').val()) < 0) {
						top.$.ligerDialog.error('结束日期必须大于开始日期！');
						return;
					}
				} else {
					if($api.isEmpty($('#Fday').val())) {
						top.$.ligerDialog.error('使用日期范围不能为空！');
						return;
					}
				}
			}

			if(ctype == 1) {
				if($api.isEmpty($('#Ffacevalue').val())) {
					top.$.ligerDialog.error('优惠金额不能为空！');
					return;
				}
				var fullNum = Number($('#Ffullvalue').val());
				var faceNum = Number($('#Ffacevalue').val());
				/*if(fullNum < faceNum) {

					top.$.ligerDialog.error('满金额必须大于优惠金额！');
					return;
				}*/
			} else if(ctype == 2) {
				if($api.isEmpty($('#Ffacevalue2').val())) {
					top.$.ligerDialog.error('面值金额不能为空！');
					return;
				}
				fullNum = Number($('#Ffullvalue2').val());
				faceNum = Number($('#Ffacevalue2').val());
				if(fullNum > faceNum) {
					top.$.ligerDialog.error('实付金额必须小于面值金额！');
					return;
				}
			}
			var param;
			var pids = "";
			if(ctype == 1) {

				for(var rowid in manager.rows) {
					var row = manager.getRow(rowid);
					pids += row.Fid + ",";
				}
				if(pids.length > 0) {
					pids = pids.substr(0, pids.length - 1);
				}
			}

			if(!$api.isEmpty(data)) {
				var param = $("form").serializeObject();
				if(ctype == 2) {

					param.Ffacevalue = param.Ffacevalue2;
					param.Ffullvalue = param.Ffullvalue2;
				}
				param.Fid = data.Fid;
				param.Fpids = pids;
				$api.ajaxapp2(couponUrl + "&action=edit", param, function(data) {
					$api.showtoast("保存成功！");
					clear();
				});

			} else {
				var param = $("form").serializeObject();;
				param.Fpids = pids;
				if(ctype == 2) {

					param.Ffacevalue = param.Ffacevalue2;
					param.Ffullvalue = param.Ffullvalue2;
				}
				$api.ajaxapp2(couponUrl + "&action=add", param, function(data) {
					$api.showtoast("保存成功！");
					clear();
				});

			}

		}

		function clear() {
			$('#Fsummary').val("");
			$('#Fday').val("0");
			$('#Ffullvalue2').val("");
			$('#Ffullvalue').val("");
			$('#Ffacevalue').val("");
			$('#Ffacevalue2').val("");
			for(var i in manager.rows) {
				manager.remove(i);
			}
		}
	</script>

	<body class="mainbody">
		<form id="form">

			<!--内容-->
			<div class="div-content">
				<div class="divhead">优惠券信息</div>
				<hr class="divline" />
				<div class="alarm">
					友情提示：优惠券提交上线将无法进行修改，请认真检查资料是否填写完整。
				</div>
				<dl>
					<dt>*摘要</dt>
					<dd>
						<div style="float:left">
							<input type="text" id="Fsummary" name="Fsummary" maxlength="30" ltype="text" ligerui="{width:300}" validate="{required:true}" />

						</div>
						<div style="float:left">
							<span id="lnum">*摘要最多30个字符</span> </div>
					</dd>
				</dl>
				<dl>
					<dt>*类别</dt>
					<dd>
						<select name="Fctype" id="Fctype" ltype="select" ligerui="{width:180;disabled:true;}">
							<option value="1" selected="selected">优惠券</option>
							<option value="2">充值劵</option>
						</select>
					</dd>
				</dl>
				<div id="timeType">
					<dl>
						<dt>*时间类别</dt>
						<dd>
							<select name="Fsdtype" id="Fsdtype" ltype="select" ligerui="{width:180;disabled:true;}">

								<option value="2" selected="selected">按获取日</option>
								<option value="1">按截止日</option>
							</select>
						</dd>
					</dl>
					<dl id="day">
						<dt>*消费有效</dt>
						<dd>
							<div style="float:left">
								<input ltype="text" id="Fday" name="Fday" ligerui="{width:120,digits:true }" />
							</div>
							<div style="float:left;margin-left:5px;">天（领取后几天内消费有效）</div>
						</dd>
					</dl>
					<dl id="muitFday" style="display:none">
						<dt>*日期区间</dt>
						<dd>
							<div style="float:left">
								<input ltype="date" id="Fsdate" name="Fsdate" ligerui="{width:120}" />
							</div>
							<div style="float:left;margin: 0 5px;">至</div>
							<div style="float:left">
								<input ltype="date" id="Fedate" name="Fedate" ligerui="{width:120}" />
							</div>
							<div style="float:left;margin-left:5px;">该时间区间消费,过去作废</div>
						</dd>
					</dl>
				</div>
				<dl>
					<dt>人次限量</dt>
					<dd>
						<div style="float:left">
							<input ltype="text" value="1" id="Flimitnum" name="Flimitnum" ligerui="{width:120,digits:true}" />
						</div>
						<div style="float:left;margin-left:5px;">即单账户最多领取次数，0则不限次数</div>
					</dd>
				</dl>
				<dl>
					<dt>数量</dt>
					<dd>
						<div style="float:left">
							<input ltype="text" value="0" id="Fstocknum" name="Fstocknum" ligerui="{width:120,digits:true}" />
						</div>
						<div style="float:left;margin-left:5px;">数量为0时,则不限数量</div>
					</dd>
				</dl>
				<dl id="dy" style="display: none ;">
					<dt>*面值</dt>
					<dd>
						<span style="float:left;margin-left:5px;">实付</span>
						<div style="float:left;margin-left:5px;">

							<input ltype="text" id="Ffullvalue2" name="Ffullvalue2" ligerui="{width:80,currency:true}" />
						</div>
						<span style="float:left;margin-left:5px;">,抵</span>
						<div style="float:left;margin-left:5px;">

							<input ltype="text" id="Ffacevalue2" name="Ffacevalue2" ligerui="{width:80,currency:true}" />
						</div>
						<div style="float:left;margin-left:5px;">金额,(单位:人民币元)</div>

					</dd>
				</dl>
				<dl id="yh">
					<dt>*金额区间</dt>
					<dd>
						<span style="float:left;margin-left:5px;">消费满</span>
						<div style="float:left;margin-left:5px;">

							<input ltype="text" id="Ffullvalue" name="Ffullvalue" ligerui="{width:80,currency:true}" />
						</div> <span style="float:left;margin-left:5px;">,减</span>
						<div style="float:left;margin-left:5px;">

							<input ltype="text" id="Ffacevalue" name="Ffacevalue" ligerui="{width:80,currency:true}" />
						</div>
						<div style="float:left;margin-left:5px;">消费满不填时,则不无门槛,(单位:人民币元)</div>

					</dd>
				</dl>
			</div>

		</form>
		<!--内容-->
		<div id="product" class="div-content">
			<div class="divhead">商品（如是全场，则忽略不选）</div>
			<hr class="divline" />
			<div  class="toolbar">
				<div class="l-list" style="margin-left:5px;">
					<ul id="icon-list" class="icon-list">
						<li>
							<a class='add' onclick='add();'><i></i><span>添加商品</span></a>
						</li>
					</ul>
				</div>
			</div>
			<div id="maingrid4" style="margin: 0px;background:#fff;"></div>
		</div>
		<div id="btnsave" style="margin:20px;" align="center">
			<input style="width:120px;display:inline;margin-left:15px;" type="button" ID="btnSave" value="保存" class="btn yellow" onclick="doSaveInfo();" />

		</div>
	</body>

</html>